package com.example.TemplatePattern;

import com.example.TemplatePattern.entry.Request;
import com.example.TemplatePattern.entry.Response;
import com.example.TemplatePattern.service.SecurityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <pre>
 * 功能描述：
 * </pre>
 *
 * @author: 李明杰
 * @date: 2022/12/28 20:47
 */
@Component
@Slf4j
public class SecurityReportTemplate {

    @Resource
    protected SecurityService securityService;

    public Response<?> reportTemplate(RiskReportRunner riskReportRunner, Request req) {

        boolean isSuccess = true;
        Response<?> response = null;

        try {
            response = riskReportRunner.reportWithBizRunner();
        } catch (BizException bizException) {

            log.warn("bizType :{}, operate BizException", req.getBizType(), bizException);
            isSuccess = false;
            response = new Response<>().fail("", bizException.getCode(), bizException.getMsg());
            throw bizException;
        } catch (Exception e) {

            log.warn("bizType :{}, operate system,Exception", req.getBizType(), e);
            isSuccess = false;
            response = new Response<>().fail("", "666", "系统异常");
            throw e;

        } finally {
            log.info("report security,bizType:{}", req.getBizType());
            securityService.report(req, isSuccess, response);
        }

        return response;
    }
}